package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.CustomerAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.CustomerMainOriginal;
import com.isunland.managesystem.entity.CustomerMainOriginal.rCustomerMain;
import com.isunland.managesystem.entity.ExtraValueContent;
import com.isunland.managesystem.utils.ParamsNotEmpty;
import com.isunland.managesystem.utils.SharedPreferencesUtil;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * 客户管理
 *
 * @author wangyanan
 */
public class CustomerListFragment extends BaseListFragment {
  public final static String TAG = CustomerListFragment.class.getSimpleName();

  private CustomerAdapter mAdapter;
  private String customerName;
  private String chargeManIds;
  private String customerStatus;
  private String chargeManNames;
  private String customerStatusName;
  private int REQUEST_QUERY = 0;
  private int REQUEST_ADD = 1;
  private ArrayList<rCustomerMain> mList;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTitleCustom(R.string.customer_namelist);
    customerName = "";
    chargeManIds = "";
    chargeManNames = "";
    customerStatus = "";
    customerStatusName = "";
    mList = new ArrayList<>();
    mAdapter = new CustomerAdapter(mActivity, mList);
  }

  @Override public void initView(View view, Bundle savedInstanceState) {
    super.initView(view, savedInstanceState);
    setListAdapter(mAdapter);
    mListview.setDividerHeight(0);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);

    inflater.inflate(R.menu.menu_add_search, menu);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

      case android.R.id.home:
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        break;
      case R.id.menu_item_add:

        Intent add = new Intent(getActivity(), CustomerDetailActivity.class);
        add.putExtra(CustomerDetailFragment.EXTRA_TYPE, CustomerDetailFragment.VALUE_ADD);
        startActivityForResult(add, REQUEST_ADD);
        return true;
      case R.id.menu_item_querys:
        ExtraValueContent content = new ExtraValueContent();
        content.setExtName1(customerName);
        content.setExtCode2(chargeManIds);
        content.setExtName2(chargeManNames);
        content.setExtCode3(customerStatus);
        content.setExtName3(customerStatusName);
        Intent query = new Intent(getActivity(), CustomerListSearchActivity.class);
        query.putExtra(CustomerListSearchFragment.EXTRA_VLAUE, content);
        startActivityForResult(query, REQUEST_QUERY);
        return true;
      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  @Override public String getUrl() {
    return ApiConst.URL_CUSTOMER_LIST;
  }

  @Override public HashMap<String, String> getparameters() {
    ParamsNotEmpty params = new ParamsNotEmpty();
    params.put("memberCode", CurrentUser.newInstance(getActivity()).getMemberCode());
    params.put("customerName", customerName);
    params.put("chargeManIds", chargeManIds);
    params.put("customerStatus", customerStatus);
    params.put("ifCanShare", "T");
    return params.getParams();
  }

  @Override public void resolveResponse(String arg0) {

    mList.clear();
    mList.addAll(new Gson().fromJson(arg0, CustomerMainOriginal.class).getRows());
    mAdapter.notifyDataSetChanged();
  }

  public void onListItemClick(ListView l, View v, int position, long id) {

    Intent i = new Intent(getActivity(), CustomerInfoActivity.class);
    // //客户id
    rCustomerMain item = mAdapter.getItem(position - 1);
    if (item == null) return;
    SharedPreferencesUtil.setString(getActivity(), SharedPreferencesUtil.CUSTOMER_ID, item.getId());
    SharedPreferencesUtil.setString(getActivity(), SharedPreferencesUtil.CUSTOMER_NAME,
        item.getCustomerName());

    rCustomerMain customerDetail = ((CustomerAdapter) getListAdapter()).getItem(position - 1);
    i.putExtra(CustomerInfoFragment.EXTRA_VALUE, customerDetail);

    startActivityForResult(i, REQUEST_ADD);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode != Activity.RESULT_OK) {

      return;
    }

    if (requestCode == REQUEST_QUERY) {
      ExtraValueContent content =
          (ExtraValueContent) data.getSerializableExtra(CustomerListSearchFragment.EXTRA_VLAUE);
      customerName = content.getExtName1();
      chargeManIds = content.getExtCode2();
      chargeManNames = content.getExtName2();
      customerStatus = content.getExtCode3();
      customerStatusName = content.getExtName3();
      volleyPost();
    }
    if (requestCode == REQUEST_ADD) {
      volleyPost();
    }

    super.onActivityResult(requestCode, resultCode, data);
  }
}
